//
//  SingerDetailViewController.m
//  MusicPlayer
//
//  Created by apple on 15/5/23.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "SingerDetailViewController.h"
#import "SANetWorkingTask.h"
#import "SingerModel.h"
#import "SingerDetailTableViewCell.h"
#import <UIImageView+WebCache.h>
#import "SingerThirdViewController.h"


@interface SingerDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray * array;
@property (nonatomic, retain) UITableView * tableView;
@property (nonatomic, retain) UIImageView *myImageView;

@end

@implementation SingerDetailViewController

-(void)dealloc
{
    [_singerId release];
    [_array release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
    }
    return self;
}


-(void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:YES];
    [self.myImageView removeFromSuperview];
    
    //    self.navigationItem.title = @"音乐库";
    //    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = self.aTitle;
    [self.view setBackgroundColor:[UIColor whiteColor]];
    //
    self.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    //
    
    NSUserDefaults *aa = [NSUserDefaults standardUserDefaults];
    
    
    if ([aa objectForKey:@"pic1"]) {
        
        //    if (aa) {
        UIImage *image = [UIImage imageNamed:[aa objectForKey:@"pic1"]];
        
        [_myImageView setImage:image];
    } else {
        [_myImageView setImage:[UIImage imageNamed:@"PIC2.png"]];
        
    }
    
    [self.view addSubview:_myImageView];
    [self.view bringSubviewToFront:self.tableView];
  
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self SingerTableView];
    [self.view setBackgroundColor:[UIColor clearColor]];
    
    UIBarButtonItem *exits = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png"] style:UIBarButtonItemStyleDone target:self action:@selector(exitsAction:)];
    self.navigationItem.leftBarButtonItem = exits;
    
    NSString * url = [NSString stringWithFormat:@"http://v1.ard.tj.itlily.com/ttpod?id=%@&size=1000&page=1&bundle_id=com.ttpod.music",self.singerId];
    [SANetWorkingTask request:url parmater:nil block:^(NSDictionary *result) {
        
        self.array = [result objectForKey:@"data"];
        
        [_tableView reloadData];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - 返回图片的事件
- (void)exitsAction:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark 详细tableView

-(void)SingerTableView
{

    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width , self.view.frame.size.height - self.view.frame.size.height/667.0 * 64 -self.view.frame.size.height/667.0 * 70) style:UITableViewStylePlain];

    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width , self.view.frame.size.height - 64 - 70) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];

    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.rowHeight = self.view.frame.size.width / 375.0 * 120;
    [self.view addSubview:self.tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    重定池
    static NSString * cellIdentify = @"cell";
    SingerDetailTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (!cell) {
        cell = [[[SingerDetailTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentify] autorelease];
    }
    cell.backgroundColor = [UIColor clearColor];
    NSMutableDictionary * dic = [self.array objectAtIndex:indexPath.row];
    cell.aLabel.text = [dic objectForKey:@"singer_name"];
    cell.aLabel.textColor = [UIColor whiteColor];
    NSString * url = [dic objectForKey:@"pic_url"];
    [cell.imageview sd_setImageWithURL:[NSURL URLWithString:url]];
    return cell;
}

//点击进入详情

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SingerThirdViewController * third = [[SingerThirdViewController alloc]init];
    NSMutableDictionary * dic = [self.array objectAtIndex:indexPath.row];
    
    third.SingerDetailId = [dic objectForKey:@"singer_id"];
    third.aTitle = [dic objectForKey:@"singer_name"];
    [self.navigationController pushViewController:third animated:YES];
    
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
